<title>错误日志</title>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>系统监控</cite></a>
        <a><cite>日志监控</cite></a>
        <a><cite>错误日志</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 5px;">
                    <fieldset class="layui-elem-field layui-field-title"> <legend>文件</legend></fieldset>
                    <ul class="layui-menu" id="LAY-maintain-logs-error-menus" style="min-height:300px;">
                        <div class="layui-none">无数据</div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table id="LAY-maintain-logs-error-manage" lay-filter="LAY-maintain-logs-error-manage"></table>
                    <script type="text/html" id="LAY-maintain-logs-error-toolbar">
                        <div class="layui-btn-container">
                            {{# if (AppGeneral.userCan('maintain.logs.error.download')){ }}
                            <btton class="layui-btn layui-btn-sm" lay-event="download">下载</btton>
                            {{# } }}
                            {{# if (AppGeneral.userCan('maintain.logs.error.clean')){ }}
                            <btton class="layui-btn layui-btn-sm layui-btn-normal" lay-event="clean">清空</btton>
                            {{# } }}
                            {{# if (AppGeneral.userCan('maintain.logs.error.destroy')){ }}
                            <btton class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</btton>
                            {{# } }}
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['index', 'baseMethods'], function (){
        let {$, layer, table, admin, baseMethods}= layui, tableId = 'LAY-maintain-logs-error-manage', current = '';
        const tableIns = baseMethods.table({
            elem: '#'+tableId,
            page: false,
            url: '/maintain/logs/error/list',
            toolbar: '#LAY-maintain-logs-error-toolbar',
            cols: [[
                {field: 'level', title: 'Level', width: 90,
                    templet: function (d){
                        switch (d.level) {
                            case 'error':
                                return '<span class="layui-badge">error</span>';
                            case 'warn':
                                return '<span class="layui-badge layui-bg-orange">warn</span>';
                            case 'info':
                                return '<span class="layui-badge layui-bg-blue">info</span>';
                            case 'debug':
                                return '<span class="layui-badge layui-bg-green">debug</span>'
                            default:
                                return d.level;
                        }
                    }
                },
                {field: 'context', title: 'Context', width: 100},
                {field: 'date', title: 'Date', width: 180, templet: function (d) { return d.date !== 1 ? d.date : ''; }},
                {field: 'text', title: 'TEXT'},
            ]]
        });
        getMenus(function (){
            $("#LAY-maintain-logs-error-menus").on("click", 'li', function () {
                let options = AppGeneral.options($(this).attr('lay-options'));
                current = options.name;
                tableIns.reloadData({
                    where: {current_file: options.name},
                });
            });
        });
        function getMenus(fun){
            baseMethods.request({
                url: '/maintain/logs/error/files',
                done: function (result){
                    if (result.code === 200){
                        let data = result.data, html = '';
                        current = data.current;
                        if (data.rows.length > 0){
                            layui.each(data.rows, function(index, item) {
                                html += '<li lay-options="{id: '+item.id+', name: \''+item.name+'\'}" '+ (current === item.name ? 'class="layui-menu-item-checked"' : '') +'>\n <div class="layui-menu-body-title"> '+item.name+' </div>\n </li>\n';
                            })
                            $('#LAY-maintain-logs-error-menus').html(html);
                        }
                    }
                    "function" == typeof fun && fun(result);
                }
            })
        }
        let tableTools = {
            download: function(){
                if (!current) return false;
                layer.confirm('确认要下载？', {icon: 3}, function(index) {
                    layer.close(index);
                    window.open(baseMethods.apiUrl('/maintain/logs/error/download')+'?fileName='+ current +'&token=' + AppGeneral.localStorage.getItem('TOKEN'), '_blank');
                });
            },
            clean: function(){
                if (!current) return false;
                baseMethods.toolOpera({
                    title: `确定清空 ${current} 吗？`,
                    request: {
                        url: '/maintain/logs/error/clean',
                        type: "post",
                        data: {fileName: current},
                        success: function (){
                            tableIns.reloadData();
                        }
                    },
                })
            },
            delete: function () {
                if (!current) return false;
                baseMethods.toolOpera({
                    title: `确定删除 ${current} 吗？`,
                    request: {
                        url: '/maintain/logs/error/destroy',
                        type: "delete",
                        data: {fileName: current},
                        success: function (){
                            getMenus(function (){
                                tableIns.reloadData();
                            });
                        }
                    },
                })
            },
        };
        table.on('toolbar('+tableId+')', function(obj){
            tableTools[obj.event] ? tableTools[obj.event].call(this, obj) : '';
        });
        table.on('rowDouble('+tableId+')', function(obj){
            const data = obj.data;
            if (data.stack) {
                admin.popupRight({
                    title: 'STACK',
                    id: 'LAY-popup-maintain-logs-error-show',
                    area: ['35%', 'auto'],
                    success: function(){
                        $('#'+ this.id).html('<div style="padding: 20px;">'+data.stack+'</div>');
                    }
                });
            }
        });
    });
</script>
